﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using Bll;
using System.Data;

namespace Accounting.admin.DPD
{
    public partial class DepartmentAdmin : System.Web.UI.Page
    {
        BllPublic b = new BllPublic();
        ModelPublic m = new ModelPublic();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Type"] != null)
                {
                    m.UserType = Session["Type"].ToString();
                    lblUserType.Text = Session["Type"].ToString();

                    m.NavId = "2";
                    sqlshow();
                }
            }
        }
        public void sqlshow()
        {
            m.NavId = "2";
            m.UserType = lblUserType.Text;

            Newsshow.DataSource = b.Information_Inquire_All(m);
            Newsshow.DataBind();

            DataTable nav = b.Nav_Inquire_All();

            foreach (DataRow my in nav.Rows)
            {
                if (my["Id"].ToString() == "2")
                {
                    H2_Title.InnerText = "管理发布：" + my["Name"].ToString();
                }
            }

        }
        protected void Newsshow_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.Newsshow.PageIndex = e.NewPageIndex;

            if (search.Text == "Search...标题、作者")
            {
                sqlshow();
            }
            else
            {
                Retrieve();
            }
        }

        protected void Newsshow_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#777777'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
            }
        }

        protected void lbtnSelectAll_Click(object sender, EventArgs e)
        {
            //全选
            for (var i = 0; i < Newsshow.Rows.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)this.Newsshow.Rows[i].FindControl("CheckDelete");
                chkSelect.Checked = true;
            }
        }
        protected void lbtnCancel_Click(object sender, EventArgs e)
        {
            //取消
            for (var i = 0; i < Newsshow.Rows.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)this.Newsshow.Rows[i].FindControl("CheckDelete");
                chkSelect.Checked = false;
            }
        }
        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            int f = 0;
            int num = 0;
            for (var i = 0; i < Newsshow.Rows.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)this.Newsshow.Rows[i].FindControl("CheckDelete");
                if (chkSelect.Checked)
                {
                    num++;
                    m.Id = ((Label)Newsshow.Rows[i].FindControl("lblId")).Text;

                    bool del = b.Information_Delete(m);
                    if (del)
                    {
                        f++;
                    }
                }
            }
            if (f > 0)
            {
                Response.Write("<script>alert('删除成功！\\n您，共选中" + num + "条记录\\n共删除" + f + "条记录')</script>");
                if (search.Text == "Search...标题、作者")
                {
                    sqlshow();
                }
                else
                {
                    Retrieve();
                }
            }
            else
            {
                Response.Write("<script>alert('删除失败！')</script>");
                if (search.Text == "Search...标题、作者")
                {
                    sqlshow();
                }
                else
                {
                    Retrieve();
                }
            }
        }

        protected void lbtnReverse_Click(object sender, EventArgs e)
        {
            //反选
            for (var i = 0; i < Newsshow.Rows.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)this.Newsshow.Rows[i].FindControl("CheckDelete");
                if (chkSelect.Checked)
                {
                    chkSelect.Checked = false;
                }
                else
                {
                    chkSelect.Checked = true;
                }
            }
        }
        protected void IbtnSearch_Click(object sender, EventArgs e)
        {
            Retrieve();
        }

        private void Retrieve()
        {
            m.NavId = "2";
            m.UserType = lblUserType.Text;

            if (search.Text == "Search...标题、作者")
                m.Conditions = "";
            else
                m.Conditions = search.Text.Trim();

            Newsshow.DataSource = b.Information_Search_All(m);
            Newsshow.DataBind();
        }
    }
}